package com.alan.product.issue.content;

import com.alan.product.issue.entity.Order;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.SearchHit;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@Slf4j
public class ElasticsearchTest {

    private static List<RestHighLevelClient> clients = new ArrayList<>();

    static {
        for (int i = 0; i < 10; i++) {
            clients.add(new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost(
                                    "localhost",
                                    9200,
                                    "http")
                    )
            ));
        }
    }


    public static void main(String[] args) throws IOException {

        search();

//        for (int i = 0; i < 100; i++) {
//            new Thread(() -> {
//                while (true) {
//                    add();
//                }
//            }).start();
//        }
//
//        add();

//        query(1L);

    }

    private static void search() throws IOException {
        SearchRequest request = new SearchRequest("product-issue-2021-08-01");
        SearchResponse response = randomClient().search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : response.getHits()) {
            System.out.println(hit.toString());
        }

    }

    private static void query(Long id) throws IOException {
        GetRequest getRequest = new GetRequest("order", String.valueOf(id));
        GetResponse response = randomClient().get(getRequest, RequestOptions.DEFAULT);

        System.out.println(response.getSourceAsString());
    }

    private static void add() {
        Order order = new Order();
        order.setId(ThreadLocalRandom.current().nextLong(100000000));
        order.setOrderNo("123456");

        IndexRequest indexRequest = new IndexRequest("order");
        indexRequest.id(String.valueOf(order.getId()));
        indexRequest.source(JSON.toJSONString(order), XContentType.JSON);

        try {
            IndexResponse response = randomClient().index(indexRequest, RequestOptions.DEFAULT);
            System.out.println(response.toString());
        } catch (Exception e) {
            log.error("write data error", e);
        }
    }

    private static RestHighLevelClient randomClient() {
        return clients.get(ThreadLocalRandom.current().nextInt(10000) % 10);
    }
}
